package com.example.proxy.cglib;

import com.example.proxy.cglib.service.Calculate;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class Main {
    public static void main(String[] args) {
        Enhancer enhancer=new Enhancer();
        enhancer.setSuperclass(Calculate.class);
        enhancer.setCallback(new MethodInterceptor() {
            @Override
            public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
                System.out.println("执行前代理");
                Object o1 = methodProxy.invokeSuper(o, objects);
                System.out.println("后置增强");
                return o1;
            }
        });

        Calculate calculate = (Calculate)enhancer.create();
        int cal = calculate.cal(13);
        System.out.println(cal);
    }
}
